<?php
namespace app\agent\controller;
use app\common\controller\AdminBase;
use app\common\model\Agent;
use app\common\model\WxOpen;
use app\common\model\Merchant;
use app\common\model\System;

use think\Controller;
use think\Session;
use Wechat\Wxobjopen;
class Uploads extends Controller
{
    public function upload()
    {
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/card/');

        if($info){
            $imgurl['pic'] = '/uploads/card/'.$info->getSaveName();
            $imgurl['name'] = $info->getSaveName();
            $imgurl['ext'] = $info->getExtension();
    //        $res = $this->uploadwxcard($imgurl['pic']);
            $data['pic'] = $imgurl['pic'];
            $data['code'] = 0;
            return json($data);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    public function machuploads(){
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/mach/');
        if($info){
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

            $imgurl =$http_type.$_SERVER['HTTP_HOST'] .'/uploads/mach/'.$info->getSaveName();
            $data['pic'] =$imgurl;
            $data['code'] = 0;
            return json($data);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    //上传证书
    public function uploadpem(){
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'pem'])->move(EXTEND_PATH . 'Weixinpay/cert/');

        if($info){
            $pem['url'] = EXTEND_PATH . 'Weixinpay/cert/'.$info->getSaveName();
            $pem['ext'] = $info->getExtension();
            $data['url'] = $pem['url'];
            $data['code'] = 0;
            return json($data);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    //excel上传
    public function uploadexcel(){
        $file = request()->file('excel');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['ext'=>'xls,xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads/excel/');
        if ($info){
            $excel['code'] = 0;
            $excel['url'] = '/uploads/excel/'. $info->getSaveName();
            return json($excel);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    //微信上传
    public function uploadwxcard($img){
        $data = ["buffer" => '@.'.$img];
        $option = $this->option();
        $wxObj = new Wxobjopen($option);
        $res = $wxObj->uploadImg($data);
    //    $res = json_decode($res,true);
        return $res;
    }


    //微信API参数
    public function option(){
        Session::set('openid', null);
        $map['top_mid'] = Session::get('login_top_mid');
        $System=new System();
        $WxOpen=new WxOpen();
        $config =$System->where(array('id' => 1))->find();
        $users = $WxOpen->where($map)->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }

    public function uploadbase64(){
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/mach/');
        if($info){
            $imgurl = $_SERVER['DOCUMENT_ROOT'].'/uploads/mach/'.$info->getSaveName();
            $data['pic'] = $this->base64EncodeImage($imgurl);
            $data['code'] = 0;
            return ($data);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    public function base64EncodeImage ($image_file) {
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file,'r'), filesize($image_file));
        $base64_image = 'data:'.$image_info['mime'].';base64,'.base64_encode($image_data);
//        return str_replace(array("↵"), "", $base64_image);
        return $base64_image;
    }
}
